
#include "getsDispose.h"




void trainTransfileGets(int fd,int sockfd){
    printf("into trainTransfileGets\n");
    Train train;
    while(1){
        int rret = recv(sockfd,&train.length,sizeof(train.length),MSG_WAITALL);
        if(rret == 0 || train.length == 0){
            printf("trainTransfileGets out\n");
            break;
        }
        recv(sockfd,train.data,train.length,MSG_WAITALL);
        write(fd,train.data,train.length);
    }
    printf("out trainTransfileGets\n");
}

void mmapTransfileGets(int fd,int sockfd,long filesize){
    printf("into mmapTransfileGets\n");
    ftruncate(fd,filesize);
    char * p = (char *)mmap(NULL,filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    recv(sockfd,p,filesize,MSG_WAITALL);
    msync(p,filesize,MS_SYNC);
    munmap(p,filesize);
    printf("out mmapTransfileGets\n");
    return;
}

int getsDispose(int sockfd,char * spath,char * cpath){   //str表示你要服务器哪个位置的文件
    
    char command[] = "gets";
    Train train;
    train.length = strlen(command);            //向后台发送信号
    memcpy(train.data,command,train.length);
    send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);

                        //发路径   /fs/fsd/fsd
    
    train.length = strlen(spath);
    memcpy(train.data,spath,train.length);
    send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    
    int length = strlen(spath);                   //割出文件名(留给后面客户端没有这个文件时，创建文件)
    int i = length - 1;
    while(i>=0){                    
        if(spath[i] == '/'){
            break;
        } 
        --i;
    }
    char filename[64] = {0};
    if(i == -1){
        strcpy(filename,spath); 
    }else{
        for(int j = i+1 , k = 0; j < strlen(spath) ; ++j , ++k){
            filename[k] = spath[j];
        }
    }
    char xiegang[] = "/";
    strcat(cpath,xiegang);
    strcat(cpath,filename);     //得到文件要存放的位置，打开文件
    
    int epollfd = epoll_create(1);
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = sockfd;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,sockfd,&event);
    struct epoll_event readyset[5];
    epoll_wait(sockfd,readyset,5,-1);         //等待服务端发送有没有信号
                                              //
                                              

    char has;                                 //接收是否有
    recv(sockfd,&has,1,MSG_WAITALL);
    printf("接收has:%c\n",has);
    if(has == '0'){
        printf("no find file\n");
        return 0;
    }
    if(has == '1'){
        char isBig;                                 //接收是不是大文件                             
        recv(sockfd,&isBig,1,MSG_WAITALL);
        printf("isBig:%c\n",isBig);

        long filesize = 0;                          //接收文件大小
        recv(sockfd,&train.length,sizeof(train.length),MSG_WAITALL);
        recv(sockfd,train.data,train.length,MSG_WAITALL);
        memcpy(&filesize,train.data,train.length);
        printf("filesize:%ld\n",filesize);

        int fd = open(cpath,O_RDWR|O_CREAT|O_TRUNC,0666);    //在当前文件夹下打开并清空文件
        printf("fd:%d\n",fd);

        if(isBig == '1'){
            mmapTransfileGets(fd,sockfd,filesize);
        }
        if(isBig == '0'){
            trainTransfileGets(fd,sockfd);
        }
    }

    return 0;
} 


